﻿/// Author:					Joe Audette
/// Created:				2007-02-15
/// Last Modified:		    2009-07-27
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.

using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using Resources;
using System.Collections;
using ud_Links.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using ud_Utilities;

namespace ud_Links.UI
{
    public partial class AdminLinkEdit : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private Guid itemGuid = Guid.Empty;
        private ud_LinkDB linkDB;
       
        protected string EditContentImage = WebConfigSettings.EditContentImage;
        protected string DeleteLinkImage = WebConfigSettings.DeleteLinkImage;
        protected string DeleteLinkText = LinkModuleResource.ButtonDelete;
        protected string DeleteLinkImageUrl = string.Empty;
        protected string EditLinkText = LinkModuleResource.ButtonEdit;
        protected string EditLinkImageUrl = string.Empty;
        

        void Page_Load(object sender, EventArgs e)
        {
            if (!UserCanEditPage() || !UserCanViewPage())
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            if (!Page.IsPostBack)
            {
                LoadSettings();
            }
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            BindData();
        }

        private void LoadSettings()
        {
            
        }

        private void PopulateLabels()
        {
            moduleTitle.Header = LinkModuleResource.LinksFeatureName;
            //BindEditor();
            btnDelete.Text = LinkModuleResource.ButtonDelete;
            btnSave.Text = LinkModuleResource.ButtonSave;
            gvItems.Columns[0].HeaderText = LinkModuleResource.LabelName;
            gvItems.Columns[1].HeaderText = LinkModuleResource.LabelLink;
            EditLinkImageUrl = ImageSiteRoot + "/Data/SiteImages/" + EditContentImage;
            DeleteLinkImageUrl = ImageSiteRoot + "/Data/SiteImages/" + DeleteLinkImage;
            if (itemGuid.Equals(Guid.Empty))
            {
                btnDelete.Visible = false;
            }
            Utilities.AddConfirmButton(btnDelete, LinkModuleResource.WarningDeleteComfirm);
        }


        void btnDelete_Click(object sender, EventArgs e)
        {
            
            linkDB.Detele(itemGuid);
            Refresh();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            if (itemGuid.Equals(Guid.Empty))
            {
                linkDB.Insert(txtName.Text, txtLink.Text);
            }
            else
            {
                linkDB.Update(itemGuid,txtName.Text, txtLink.Text);
                itemGuid = Guid.Empty;
            }

            Refresh();
        }

        private void Refresh()
        {
            WebUtils.SetupRedirect(this, SiteRoot + "/Links/Admin/AdminLinkEdit.aspx?pageid=" + pageId.ToInvariantString() + "&mid=" + moduleId.ToInvariantString());
        }

        private void ClearEdit()
        {
            txtName.Text = string.Empty;
            txtLink.Text = string.Empty;
        }

        void gvItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EditItem"))
            {
                itemGuid = new Guid(e.CommandArgument.ToString());
                WebUtils.SetupRedirect(this, SiteRoot + "/Links/Admin/AdminLinkEdit.aspx?pageid=" 
                    + pageId.ToInvariantString() + "&mid=" + moduleId.ToInvariantString() + "&itemid=" + itemGuid.ToString());
            }
            else if (e.CommandName.Equals("DeleteItem"))
            {
                itemGuid = new Guid(e.CommandArgument.ToString());
                linkDB.Detele(itemGuid);
                WebUtils.SetupRedirect(this, Request.RawUrl);
            }
        }

        void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvItems.PageIndex = e.NewPageIndex;
            BindData();
        }

        private void BindData()
        {
            var links = linkDB.GetAll();
            gvItems.DataSource = links;
            gvItems.DataBind();
        }

        void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtnDelete = e.Row.FindControl("ibtnDelete") as ImageButton;
                Utilities.AddConfirmButton(ibtnDelete, LinkModuleResource.ButtonDelete);
            }
        }

        public string FormatInfo(string info)
        {
            string result = Utilities.StripHtml(info, false);
            if(result.Length > 150)
                result = Utilities.StripHtml(info, false).Substring(0, 150);
            return result;
        }

        #region OnInit
        
        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            this.Load += new EventHandler(Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            linkDB = new ud_LinkDB(moduleId);
            base.OnInit(e);
            SuppressPageMenu();
            btnSave.Click += new EventHandler(btnSave_Click);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            gvItems.RowCommand += new GridViewCommandEventHandler(gvItems_RowCommand);
            gvItems.RowDataBound += new GridViewRowEventHandler(gvItems_RowDataBound);
            gvItems.PageIndexChanging += new GridViewPageEventHandler(gvItems_PageIndexChanging);
            this.EnableViewState = false;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
            itemGuid = WebUtils.ParseGuidFromQueryString("itemid", itemGuid);
        } 

        #endregion

    }
}
